/**
 * Created by caichong on 2015/11/12.
 */



function uploadPic(upload,pti,projectThumImage){
    'use strict';
    // Change this to the location of your server-side upload handler:
    var url = 'http://'+window.location.host+'/uploadImg.action';
    uploadPicCommon(upload,pti,projectThumImage,url);
}

function uploadPic80(upload,pti,projectThumImage) {
    'use strict';
    // Change this to the location of your server-side upload handler:
    var url = 'http://'+window.location.host+'/uploadImgSize80.action';
    uploadPicCommon(upload,pti,projectThumImage,url);
}

function uploadPicCommon(upload,pti,projectThumImage,url) {
    'use strict';
    var dataType='json';
    if ($.NV('name')=="firefox" || $.NV('name')=="chrome"){
        dataType='';
    }
    $('#'+upload).fileupload({
        dataType:dataType,
        done: function (e, data) {
            var json=data.result;
            if (data.result.files==undefined || data.result.files==null){
                var jsonStr=data.result+"";
                jsonStr=jsonStr.substring(0,jsonStr.length-64);
                json=$.parseJSON(jsonStr);
            }
            $.each(json.files, function (index, file) {
                if (file.result=="true"){
                    $("#"+pti).attr("src",file.url);
                    $("#"+projectThumImage).val(file.url);
                    alert("上传成功!");
                }else{
                    alert("上传失败:"+file.errorMsg+"!");
                }
            });
        },
        error:function(e,data){
            var json=data.result;
            try{
                if (data.result.files==undefined || data.result.files==null){
                    var jsonStr=data.result+"";
                    jsonStr=jsonStr.substring(0,jsonStr.length-64);
                    json=$.parseJSON(jsonStr);
                }
            }catch (err){
                json= e.responseText;
                if (json==null || json==undefined){
                    alert("上传失败.");
                    return;
                }
                json=json.substring(0,json.length-64);
                json=$.parseJSON(json);
            }
            $.each(json.files, function (index, file) {
                if (file.result=="true"){
                    $("#"+pti).attr("src",file.url);
                    $("#"+projectThumImage).val(file.url);
                    alert("上传成功!");
                }else{
                    alert("上传失败:"+file.errorMsg+"!");
                }
            });
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
}

function uploadPicfwb(upload,pti,projectThumImage){
    'use strict';
    // Change this to the location of your server-side upload handler:
    var url = 'http://'+window.location.host+'/uploadImg.action';
    var dataType='json';
    if ($.NV('name')=="firefox" || $.NV('name')=="chrome"){
        dataType='';
    }
    $('#'+upload).fileupload({
        dataType:dataType,
        done: function (e, data) {
            var json=data.result;
            if (data.result.files==undefined || data.result.files==null){
                var jsonStr=data.result+"";
                jsonStr=jsonStr.substring(0,jsonStr.length-64);
                json=$.parseJSON(jsonStr);
            }
            $.each(json.files, function (index, file) {
                if (file.result=="true"){
                    $("#"+pti).attr("src",file.url);
                    $("#"+projectThumImage).val(file.url);
                    UE.getEditor('editor').focus();
                    var funcName =  '<img src="' + file.url + '">';
                    UE.getEditor('editor').execCommand('inserthtml',funcName);
                }else{
                    alert("上传失败:"+file.errorMsg+"!");
                }
            });
        },
        error:function(e,data){

        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
}
